package com.kantboot.timer.run;

import com.kantboot.timer.event.GloberTimerEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 全局定时器
 */
@Slf4j
@Component
@EnableScheduling
public class GlobeTimer {

    private final ApplicationEventPublisher applicationEventPublisher;

    public GlobeTimer(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    /**
     * 全局定时器
     * 每秒钟执行一次
     */
    @Scheduled(fixedRate = 5000)
    public void globeTimer() {
        log.info("全局定时器运行中...");
        applicationEventPublisher.publishEvent(new GloberTimerEvent(this, System.currentTimeMillis()));
        log.info("全局定时器运行结束");
    }

}
